---
title: Switch to uploadthing
description: How to change the default storage provider to uploadthing.
---

[uploadthing](https://uploadthing.com) is a platform for storing files in the cloud. It's a great alternative to AWS S3 and it's free for small projects. Here's how to switch the default storage provider to uploadthing.

## 1. Swap out the required dependencies

First, uninstall the existing dependencies from the Storage package...

```sh title="Terminal"
pnpm remove @vercel/blob --filter @repo/storage
```

... and install the new dependencies...

```sh title="Terminal"
pnpm add uploadthing @uploadthing/react --filter @repo/storage
```

## 2. Update the environment variables

Next, update the environment variables across the project, for example:

```js title="apps/app/.env"
// Remove this:
BLOB_READ_WRITE_TOKEN=""

// Add this:
UPLOADTHING_TOKEN=""
```

Additionally, replace all instances of `BLOB_READ_WRITE_TOKEN` with `UPLOADTHING_TOKEN` in the `packages/env/index.ts` file.

## 3. Update the existing storage files

Update the `index.ts` and `client.ts` to use the new `uploadthing` packages:

### Storage

```ts title="packages/storage/index.ts"
import { createUploadthing } from 'uploadthing/next';

export { type FileRouter, createRouteHandler } from 'uploadthing/next';
export { UploadThingError as UploadError, extractRouterConfig } from 'uploadthing/server';

export const storage = createUploadthing();
```

### Client

```ts title="packages/storage/client.ts"
export * from '@uploadthing/react';
```

## 4. Create new SSR file

We'll also need to create a new file for the storage package to handle the Tailwind CSS classes and SSR.

```ts title="packages/storage/ssr.ts"
export { NextSSRPlugin as StorageSSRPlugin } from '@uploadthing/react/next-ssr-plugin';
```

## 5. Create a file router in your app

Create a new file in your app's `lib` directory to define the file router. This file will be used to define the file routes for your app, using your [Auth](/packages/authentication) package to get the current user.

```ts title="apps/app/app/lib/upload.ts"
import { currentUser } from '@repo/auth/server';
import { type FileRouter, UploadError, storage } from '@repo/storage';
  
export const router: FileRouter = {
  imageUploader: storage({
    image: {
      maxFileSize: '4MB',
      maxFileCount: 1,
    },
  })
    .middleware(async () => {
      const user = await currentUser();

      if (!user) {
        throw new UploadError('Unauthorized');
      }

      return { userId: user.id };
    })
    .onUploadComplete(({ metadata, file }) => ({ uploadedBy: metadata.userId }),
};
```

## 6. Create a route handler

Create a new route handler in your app's `api` directory to handle the file routes.

```ts title="apps/app/app/api/upload/route.ts"
import { router } from '@/app/lib/upload';
import { createRouteHandler } from '@repo/storage';

export const { GET, POST } = createRouteHandler({ router });
```

## 7. Update your root layout

Update your root layout to include the `StorageSSRPlugin`. This will add SSR hydration and avoid a loading state on your upload button.

```tsx title="apps/app/app/layout.tsx {4,5,7,16}"
import '@repo/design-system/styles/globals.css';
import { DesignSystemProvider } from '@repo/design-system';
import { fonts } from '@repo/design-system/lib/fonts';
import { extractRouterConfig } from '@repo/storage';
import { StorageSSRPlugin } from '@repo/storage/ssr';
import type { ReactNode } from 'react';
import { router } from './lib/upload';

type RootLayoutProperties = {
  readonly children: ReactNode;
};

const RootLayout = ({ children }: RootLayoutProperties) => (
  <html lang="en" className={fonts} suppressHydrationWarning>
    <body>
      <StorageSSRPlugin routerConfig={extractRouterConfig(router)} />
      <DesignSystemProvider>{children}</DesignSystemProvider>
    </body>
  </html>
);

export default RootLayout;
```

## 8. Update your Tailwind CSS

Update your design system's `globals.css` file to include the following:

```css title="packages/design-system/styles/globals.css"
@import "uploadthing/tw/v4";
@source "../node_modules/@uploadthing/react/dist";
```

## 9. Create your upload button

Create a new component for your upload button. This will use the `generateUploadButton` function to create a button that will upload files to the `imageUploader` endpoint.

```tsx title="apps/app/app/(authenticated)/components/upload-button.tsx"
'use client';

import type { router } from '@/app/lib/upload';
import { generateUploadButton } from '@repo/storage/client';
import { toast } from 'sonner';

const UploadButton = generateUploadButton<typeof router>();

export const UploadForm = () => (
  <UploadButton
    endpoint="imageUploader"
    onClientUploadComplete={(res) => {
      // Do something with the response
      console.log('Files: ', res);
      toast.success('Upload Completed');
    }}
    onUploadError={(error: Error) => {
      toast.error(`ERROR! ${error.message}`);
    }}
  />
);
```

Now you can import this component into your app and use it as a regular component.

## 10. Advanced configuration

uploadthing is a powerful platform that offers a lot of advanced configuration options. You can learn more about them in the [uploadthing documentation](https://docs.uploadthing.com/).

- [File Routes](https://docs.uploadthing.com/file-routes)
- [Security](https://docs.uploadthing.com/concepts/auth-security)